using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Windows.Forms;

namespace Lizk.Utilities
{
    /// <summary>
    /// Useful functions for getting system information.
    /// </summary>
    public class SystemTools
    {
        /// <summary>
        /// Determines whether or not this program is already running.
        /// </summary>
        /// <returns>true if already running and false if not.</returns>
        public static bool ProcessAlreadyRunning()
        {
            Process ThisProcess = Process.GetCurrentProcess();
            Process[] AllProcesses = Process.GetProcessesByName(ThisProcess.ProcessName);
            if (AllProcesses.Length > 1)
            {
                return true;
            }
            else
            {
                return false;
            } 
        }

        private static string cachedUserDataPath;
        /// <summary>
        /// Following method for user directory is courtesy of http://blog.kowalczyk.info/kb/getting-user-specific-application-data-directory-for-.net-winforms-apps.html
        /// </summary>
        /// <returns>Path to the useres directory.</returns>
        public static string GetUserDataPath()
        {
            if (null != cachedUserDataPath) return cachedUserDataPath;
            cachedUserDataPath = System.IO.Path.Combine(System.Windows.Forms.Application.UserAppDataPath, "CamViewer");
            string[] pathParts = cachedUserDataPath.Split(System.IO.Path.DirectorySeparatorChar);
            System.Diagnostics.Debug.Assert(pathParts.Length > 3);
            string pathSep = System.Char.ToString(System.IO.Path.DirectorySeparatorChar);
            if (pathParts.Length > 3)
                cachedUserDataPath = System.String.Join(pathSep, pathParts, 0, pathParts.Length - 2);
            return cachedUserDataPath;
        }
        /// <summary>
        /// ThreadExceptionEventHandler function. Usage: Application.ThreadException += new ThreadExceptionEventHandler(Lizk.Utilities.SystemTools.CreateDebugReport);
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void CreateDebugReport(object sender, ThreadExceptionEventArgs e)
        {
            string message = "Unhandled Exception! (" + DateTime.Now +  ")\nMessage: " +
            e.Exception.Message + "\nExceptionType: " +
            e.Exception.GetType() +
            "\nStack Trace:\n" +
            e.Exception.StackTrace + "\n\n";

            try
            {
                File.AppendAllText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location) + ".debug.txt"), message);
            }
            catch
            {

            }
            DialogResult dr = MessageBox.Show(message, "Application Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop);
            if(dr != DialogResult.Retry)
                Application.Exit();
        }

    }
}
